//
//  UpdatepassViewController.m
//  CarTown
//
//  Created by xionghao on 15/9/5.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "UpdatepassViewController.h"
#import "UserInfoUpdateResde.h"
@interface UpdatepassViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titlelable;

@property (weak, nonatomic) IBOutlet UIButton *hidebtn;
@property (weak, nonatomic) IBOutlet UITextField *pass;

@end

@implementation UpdatepassViewController
+(instancetype)viewcontroller{
    UpdatepassViewController *upvc=[[UpdatepassViewController alloc]initWithNibName:NSStringFromClass([self class]) bundle:nil];
    return upvc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.hidebtn.hidden=YES;
    self.pass.secureTextEntry=NO;
    switch (self.type) {
        case NAMETYPE:
            self.titlelable.text=@"姓名";
            self.navigationItem.title=@"修改姓名";
            self.pass.text=self.text;
            break;
        case PHONETYPE:
            self.titlelable.text=@"联系电话";
            self.navigationItem.title=@"修改联系电话";
            self.pass.keyboardType=UIKeyboardTypePhonePad;
            self.pass.text=self.text;
            break;
        case JOBTYPE:
            self.titlelable.text=@"职位";
               self.navigationItem.title=@"修改职位";
            self.pass.text=self.text;
            break;
        case PASSTYPE:
            self.titlelable.text=@"密码";
            self.navigationItem.title=@"修改密码";
            self.pass.secureTextEntry=YES;
            self.hidebtn.hidden=NO;
            self.pass.text=self.text;
            break;
        case BINDTYPE:
            self.titlelable.text=@"绑定手机号";
            self.navigationItem.title=@"修改绑定手机号";
              self.pass.keyboardType=UIKeyboardTypePhonePad;
           
            break;
        default:
            break;
    }
    
    
    
}
- (IBAction)changehide:(id)sender {
    self.pass.secureTextEntry=!self.pass.secureTextEntry;
}
- (IBAction)saveclick:(id)sender {
    
    [self.pass endEditing:YES];
    BOOL isok=[self checkForm];
    if (isok) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在修改..";
      
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
     
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        params[@"action"]=@"c_child_info";
        params[@"user_type"]=@"company";
        params[@"child_id"]=self.child_id;
        params[@"com_id"]=self.com_id;
        params[@"edit"]=@"edit";
        switch (self.type) {
            case NAMETYPE:
                params[@"tname"]=self.pass.text;
                break;
            case PHONETYPE:
                 params[@"phone"]=self.pass.text;
                break;
            case JOBTYPE:
                 params[@"job"]=self.pass.text;
                break;
            case PASSTYPE:
                params[@"pass"]=self.pass.text;
                break;
            case BINDTYPE:
               params[@"bphone"]=self.pass.text;
                break;
            default:
                break;
        }

        [PostUitls Post:userinfourl parameters:params success:^(id response) {
            hud.mode = MBProgressHUDModeText;
            UserInfoUpdateResde *mode=[UserInfoUpdateResde resdeWithDict:response];
            if ([mode.state integerValue]==400) {
                if (self.block) {
                    self.block(self.pass.text);
                }
                hud.labelText=@"修改成功";
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                hud.labelText=mode.msg;
            }
            [hud hide:YES afterDelay:1];
        } failure:^(NSError *error) {
            hud.mode = MBProgressHUDModeText;
            hud.labelText=@"网络错误";
             [hud hide:YES afterDelay:1];
        }];
        
        
    }
}
-(BOOL)checkForm{
    if (self.type==PASSTYPE&&self.pass.text.length<=6) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"密码必须大于6位";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }else if(self.type==BINDTYPE){
        return YES;
    }
    else if (self.pass.text.length==0) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"内容不能为空";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }else{
        return YES;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
